<?php

namespace front\modules\web\controllers;

use goods\services\CollectionService;
use goods\services\DCommentResultService;
use goods\services\DCommentService;
use order\services\OrderService;
use users\services\UserService;
use Yii;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\helpers\Json;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backfeed\services\CommentService;
use yii\helpers\ArrayHelper;
use goods\services\ProductService;

/**
 * CommentController implements the CRUD actions for CommentModel model.
 */
class CommentController extends WebBaseController
{
    public $layout = false;

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                     'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all CommentModel models.
     * @return mixed
     * 商品评论列表
     * www.wdt.com/comment/index?page=2&per-page=3&sort=+id
     */
    public function actionIndex()
    {
        if(!isset($_GET['per-page'])){
            $_GET['per-page'] = 8;
        }
        if(!isset($_GET['page'])){
            $_GET['page'] = 0;
        }
        $data = yii::$app->request->get();
        $data['getByWeb'] = 1;
        $product = ProductService::ProductInfo($data['product_id']);
        $product['product'] = ArrayHelper::toArray($product);
        $product = $product['product']['result']['productInfo']['item_url_map'];
        $comment['list'] =[];
        if(empty($product)){
            $param['getByWeb'] = 1;
            $param['product_id'] = $data['product_id'];
            $info = CommentService::search($param);
            if($info['code'] == 0) {
                $info['result'] = ArrayHelper::toArray($info['result']);
                \yii::info($info['result']);
                if (is_array($info['result'])) {
                    foreach ($info['result'] as $com) {
                        $arr['created_at'] = date('Y-m-d', $com['created_at']);
                        $arr['content'] = $com['content'];
                        $arr['product'] = ['product_id' => $data['product_id']];
                        $user = UserService::getUserInfo($com['user_id']);
                        $user = ArrayHelper::toArray($user['result']);
                        $mobile = $user['mobile'];
                        $replace = substr($mobile,3,4);
                        $mobile = str_replace($replace,'****',$mobile);
                        $arr['user_name'] = !empty($user['username']) ? $user['username'] : $mobile;
                        array_push($comment['list'], $arr);
                    }
                }
            }
        }else {
            foreach ($product as $key => $item) {
                if ($item['product_id'] != $data['product_id']) {
                    continue;
                }
                $item['getByWeb'] = 1;
                $info = CommentService::search($item);
                if ($info['code'] == 0) {
                    $info['result'] = ArrayHelper::toArray($info['result']);
                    \yii::info($info['result']);
                    if (is_array($info['result'])) {
                        foreach ($info['result'] as $com) {
                            $arr['created_at'] = date('Y-m-d', $com['created_at']);
                            $arr['content'] = $com['content'];
                            $arr['product'] = $product[$key];
                            $user = UserService::getUserInfo($com['user_id']);
                            $user = ArrayHelper::toArray($user['result']);
                            $mobile = $user['mobile'];
                            $replace = substr($mobile,3,4);
                            $mobile = str_replace($replace,'****',$mobile);
                            $arr['user_name'] = !empty($user['username']) ? $user['username'] : $mobile;
                            array_push($comment['list'], $arr);
                        }
                    }
                }
            }
        }
        $created_at = array();
        foreach ($comment['list'] as $item) {
            $created_at[] = $item['created_at'];
        }
        array_multisort($created_at, SORT_DESC, $comment['list']);
        $result = CommentService::getCount(['product_id'=>$data['product_id']]);
        $comment['count'] = $result['result']['count'];
        return $this->render('index',['data'=>$comment, 'product_id'=>$data['product_id']]);
    }


    public function actionList()
    {
        if(!isset($_GET['per-page'])){
            $_GET['per-page'] = 8;
        }
        if(!isset($_GET['page'])){
            $_GET['page'] = 0;
        }
        $data = yii::$app->request->get();
        $data['getByWeb'] = 1;
        $product = ProductService::ProductInfo($data['product_id']);
        $product['product'] = ArrayHelper::toArray($product);
        $product = $product['product']['result']['productInfo']['item_url_map'];
        $comment = [];
        if(empty($product)){
            $param['getByWeb'] = 1;
            $param['product_id'] = $data['product_id'];
            $info = CommentService::search($param);
            if($info['code'] == 0) {
                $info['result'] = ArrayHelper::toArray($info['result']);
                \yii::info($info['result']);
                if (is_array($info['result'])) {
                    foreach ($info['result'] as $com) {
                        $arr['created_at'] = date('Y-m-d', $com['created_at']);
                        $arr['content'] = $com['content'];
                        $arr['product'] = ['product_id' => $data['product_id']];
                        $user = UserService::getUserInfo($com['user_id']);
                        $user = ArrayHelper::toArray($user['result']);
                        $mobile = $user['mobile'];
                        $replace = substr($mobile,3,4);
                        $mobile = str_replace($replace,'****',$mobile);
                        $arr['user_name'] = !empty($user['username']) ? $user['username'] : $mobile;
                        array_push($comment, $arr);
                    }
                }
            }
        }else {
            foreach ($product as $key => $item) {
                if ($item['product_id'] != $data['product_id']) {
                    continue;
                }
                $item['getByWeb'] = 1;
                $info = CommentService::search($item);
                if ($info['code'] == 0) {
                    $info['result'] = ArrayHelper::toArray($info['result']);
                    \yii::info($info['result']);
                    if (is_array($info['result'])) {
                        foreach ($info['result'] as $com) {
                            $arr['created_at'] = date('Y-m-d', $com['created_at']);
                            $arr['content'] = $com['content'];
                            $arr['product'] = $product[$key];
                            $user = UserService::getUserInfo($com['user_id']);
                            $user = ArrayHelper::toArray($user['result']);
                            $mobile = $user['mobile'];
                            $replace = substr($mobile,3,4);
                            $mobile = str_replace($replace,'****',$mobile);
                            $arr['user_name'] = !empty($user['username']) ? $user['username'] : $mobile;
                            array_push($comment, $arr);
                        }
                    }
                }
            }
        }
        $created_at = array();
        foreach ($comment as $item) {
            $created_at[] = $item['created_at'];
        }
        array_multisort($created_at, SORT_DESC, $comment);
        echo trim($_GET['funcName']).'('.json_encode($comment).')';
        exit;
    }

    /**
     * 插入评论
     */

    public function actionAdd()
    {
        $data['product_id'] = intval(yii::$app->request->get('product_id'));
        $orderId = intval(yii::$app->request->get('order_id'));
        if(yii::$app->request->isAjax){
            $data['user_id'] = $this->getUserId();
            $data['created_at'] = time();
            $data['order_id'] = $orderId;
            $data['product_id'] = yii::$app->request->get('pid');
            $data['content'] =strip_tags(yii::$app->request->get('content'));
            $data['d_comment'] = yii::$app->request->get('d_comment');
            yii::info($data);

            $d_result = DCommentResultService::saveComment($data);
            if($d_result['code']== 0) {
                $status = OrderService::changeStatus([0=>[ 'order_id' => $orderId,
                'status' => 6,
                ]]);
                if($status['code'] != 0){
                    return $status['message'];
                }
                $result = CommentService::add($data);
                if($result['code'] != 0){
                    return $result['message'];
                }
                return 1;
            }
            return $d_result['message'];
        }
        $product = ProductService::ProductInfo($data['product_id']);
        if($product['code'] != 0) {
            return $this->redirect('/web/order');
        }
        $product = ArrayHelper::toArray($product['result']);
        $info['product']['attributes'] = $product['productInfo']['item_url_map'];
        foreach ($info['product']['attributes'] as $item) {
            if ($data['product_id'] == $item['product_id']) {
                $info['product']['attributes'] = $item;
                break;
            }
        }
        $info['product']['thumb'] = $product['productInfo']['thumb'][0] . '@1e_1c_0o_0l_200h_200w_100q.jpg';
        $info['product']['name'] = $product['productInfo']['name'];
        $info['product']['price'] = static::price_format($product['productInfo']['price']);
        $info['product_id'] = $data['product_id'];
        //查出评论项
        $d_comment = DCommentService::getProductComment($data['product_id']);
        return $this->render('add', [
            'data' => $info,
            'order_id' => $orderId,
            'd_comment' => $d_comment,
        ]);

    }


}
